#	 Copyright 2012, 2012 Noel Warren

#    This file is part of MiniVistii.
#    MiniVistii is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    MiniVistii is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.

#    You should have received a copy of the GNU Lesser General Public License
#    along with MiniVistii.  If not, see <http://www.gnu.org/licenses/>.

#This postprocessor is for a Buselatto 3006. A serious machine.  It generates the
#neccessary gcode for the machine.
module PostProcessor
	def self.makeToolpath(toolpath)
		output = []
		face = Toolbox.face(toolpath.vector)
		if face == 5
			z = "Z=s-"
			wcs = "G55"
			p = "P5"
		elsif face == 6
			z = "Z"
			wcs = "G54"
			p = "P5"
		else
			return output
		end
		if toolpath.correction == "R"
			correction = "G42"
		elsif toolpath.correction == "L"
			correction = "G41"
		else
			correction = ""
		end
		toolpath.each{|point|
			if point		
				output.push "G1" +
						"X" + Toolbox.normalize(point.x) +
						"Y" + Toolbox.normalize(point.y) +
						 z  + Toolbox.normalize(point.z)
			end			
		}
		start = "G88" + wcs + p + "T01c" + toolpath.tool[0,1] + correction
		output[0].gsub!(/G1/,start) 
		return output
	end
	
	def self.makeHole(hole)
		point = hole.point
		face = Toolbox.face(hole.vector)
		if face == 1
			p, wcs = "P1", "G54"
			x, y, z = "X", "Y=h-", "Z=s-"
		elsif face == 2
			p, wcs = "P2", "G54" 
			x, y, z = "X", "Y", "Z=s-"
		elsif face == 3
			p, wcs = "P3", "G54"
			x, y, z = "X", "Y", "Z=s-"
		elsif face == 4
			p, wcs = "P4", "G54"
			x, y, z = "X=l-", "Y", "Z=s-"
		elsif face == 5
			p, wcs = "P5", "G55"
			x, y, z = "X", "Y", "Z=s-"
		elsif face == 6
			p, wcs = "P5", "G55"
			x, y, z = "X", "Y", "Z"
		end
		output = "G81G90" + wcs + p +  
				 "R54=" + Toolbox.normalize(hole.diameter) +
				 x + Toolbox.normalize(point.x) +
				 y + Toolbox.normalize(point.y) +
				 z + Toolbox.normalize(point.z) 
		puts output
		return output
	end
	
	def self.makeWorkpiece(workpiece)
		gcode5 = []
		gcode6 = []
		workpiece.holes.each{|hole|
			if (face = Toolbox.face(hole.vector))
				if face.between? 1, 5 
					gcode5.push makeHole(hole)
				elsif face == 6
					gcode6.push makeHole(hole)
				end
			end
		}
		workpiece.toolpaths.each{|toolpath|
			if (face = Toolbox.face(toolpath.vector))
				if face == 5
					gcode5 += makeToolpath(toolpath)
				elsif face == 6
					gcode6 += makeToolpath(toolpath)
				end
			end
		}
		programs = []
		counter = 0
		[gcode6,gcode5].each{|gcode|
			(counter == 0)? face = 6 : face = 5
			counter = counter.next
			chunks = Toolbox.breakUp(gcode)
			chunks.each_index{|index|
				chunk = chunks[index] 
				chunk += CNC.programStart(workpiece)
				chunk += CNC.programEnd(workpiece)
				program = Program.new(workpiece, chunk, index, chunks.length, face)
				programs.push program
			}
		}
		return programs
	end
	
	def self.makeAssembly(assembly)
		projectName = File.basename(Dir.pwd)
		assemblyName = assembly.name
		Dir::mkdir File.join(Dir.pwd, assemblyName)
		list = []
		assembly.each{|workpiece|
			programs = makeWorkpiece(workpiece)
			programs.each{|program|
				fileName = workpiece.name + program.parts.to_s + "_" + (program.part+1).to_s
				fileName += "_R" if program.face == 6
				fileName += ".mvi"
				program.path = File.join(CNC.path, projectName, assemblyName, fileName)
				File.open(File.join(Dir.pwd, assemblyName, fileName), 'w') {|f| f.puts program.gcode}
				list.push CNC.listItem(program)
			}	
		}
		puts list
		File.open(File.join(Dir.pwd, assembly.name + ".LST"), 'w') {|f| f.puts list}
	end
	
	def self.make(assemblies)
	    if Sketchup::active_model.path != ""
			projectName = Sketchup::active_model.path.split('\\').last.chomp('.skp')
		else
			projectName = "Untitled"
		end
		if File.directory?(File.join(Dir.pwd, projectName))
			counter = 0
			while File.directory?(File.join(Dir.pwd, projectName + counter.to_s))
				counter = counter.next 
			end
			projectName = projectName + counter.to_s
		end
	    input = UI::savepanel("Generate Gcode", projectName)
	    if input
	    	puts File.directory?(input)
		    Dir.mkdir(input)
		    Dir.chdir(input)
			assemblies.each{|assembly|
				makeAssembly(assembly)
			}
			Dir.chdir("..")
			UI.messagebox("Your gcode has been generated")
		end
	end
end

puts "postprocessor OK"
